﻿(function ($) {
	$.util.namespace("loginpage");
	
	var loginBtn = "#loginBtn", btnClick = null, iforgotBtnClick = null, showErrorMsg = "#showErrorMsg", 
		captchaImage = "#captchaImage", iForgotcaptchaImage = "#iForgotcaptchaImage", countTime = null, 
		iforgotBtn = "#iforgotBtn", iforgotMsg = "#iforgotMsg", validBox = "#validBox", cookieUser = "cookieUser" ;
	
	window.loginpage.init = function() {
		$("#fullpage").fullpage({
			sectionsColor: ['#8FB98B', '#DE564B', '#F9F3DC'],
			anchors: ['login', 'iforgot', 'help'],
			slidesNavigation: true,
			navigation: false,
			menu: '#menu',
			afterRender: function(){
			},
			afterLoad: function(anchorLink, index){
			},
			onLeave: function(index, direction){
			}
		});
	};
	
	window.loginpage.captchaImage = function(){
		$(captchaImage).click(function(){
			$(this).attr("src", $.ctx+"/manager/system/user/captchaCode?d="+new Date().getTime());
		});
		$(iForgotcaptchaImage).click(function(){
			$(this).attr("src", $.ctx+"/manager/system/user/captchaCode?d="+new Date().getTime());
		});
		
		//刷新登录页面判断是否需要显示验证码
		$.get($.ctx+"/manager/system/user/isShowCaptchaCode", function(result) {
			if(!result.status) {
				$(captchaImage).attr("src", $.ctx+"/manager/system/user/captchaCode?d="+new Date().getTime());
				$(validBox).show();
			}
		});
	};
	
	
	var keydown ;
	var loginEnter;
	var loginClickBtn ;
	
	/**
	 * 回车登录事件
	 */
	window.loginpage.keydownLogin = function() {
		keydown = $(".login-box input, .login-box button").bind("keydown", loginEnter = function(e) {
			if (e.keyCode == 13) {
				validLogin();
			}
		});
	};
	
	/**
	 * 点击登录事件
	 */
	window.loginpage.clickLogin = function() {
		loginClickBtn = $(loginBtn).bind("click", btnClick = function() {
			validLogin();
		});
	};
	
	function validLogin() {
		var account = $("input[name=account]").val() ;
		var password = $("input[name=password]").val() ;
		var captchaCode = $("input[name=captchaCode]").val() ;
		var remember = $("#remember").is(":checked") ;
		
		if(account.trim().length == 0) {
			$(showErrorMsg).show().children(".msg-info").html("账号不能为空或者为空格!");
			return ;
		}
		if(password.trim().length == 0) {
			$(showErrorMsg).show().children(".msg-info").html("密码不能为空或者为空格!");
			return ;
		}
		if(!$(validBox).is(":hidden")) {
			if(captchaCode.trim().length == 0) {
				$(showErrorMsg).show().children(".msg-info").html("验证码不能为空或者为空格!");
				return ;
			}
		}
		submitNow(account, password, captchaCode);
	}
	
	function submitNow(account, password, captchaCode) {
		var clickEle = $(loginBtn);
		$.ajax({
			type : "POST",
			url : $.ctx+"/manager/system/user/login",
			data : {"account": account, "password": password, "captchaCode": captchaCode},
			beforeSend: function() {
				keydown.unbind();
				loginClickBtn.unbind();
				$(showErrorMsg).hide();
				$(clickEle).children("span").html("确认中...");
			},
			success : function(result) {
				if (typeof result === "string" && !(result instanceof Array)){  
					result = $.parseJSON(result);
				}
				if(result.status) {
					$(clickEle).removeClass("btn-1").addClass("btn-3");
					$(clickEle).children("i").attr("class", "fa fa-check").siblings("span").html("验证成功");
					
					//记录Cookie
					if (remember) {
						window.loginpage.setCookie(cookieUser, $.base64.encode(account)+"[#]"+$.base64.encode(password));
					} else {
						window.loginpage.removeCookie(cookieUser);
					}
					
					//跳转进入主页
					setTimeout("window.location.replace('"+$.ctx+"/manager/main')", 500);
					
				} else {
					keydown.bind("keydown", loginEnter);
					loginClickBtn.bind("click", btnClick);
					$(clickEle).children("i").attr("class", "fa fa-key").siblings("span").html("登录");
					$(showErrorMsg).show().children(".msg-info").html(result.message);
					
					//密码错误次数太多，显示验证码
					if(undefined != result.obj) {
						if(result.obj.showCaptchaCode) {
							$(validBox).show();
							
							//需刷新验证码
							if(undefined != result.obj.resetCaptcha && result.obj.resetCaptcha == true) {
								$(captchaImage).attr("src", $.ctx+"/manager/system/user/captchaCode?d="+new Date().getTime());
							}
						}
					}
					
				}
			},
			complete: function () {},
		    error: function (data) {
		    	keydown.bind("keydown", loginEnter);
		    	loginClickBtn.bind("click", btnClick);
				$(clickEle).children("i").attr("class", "fa fa-key").siblings("span").html("登录");
		        alert("发生错误["+data.status+"]");
		    }
		}, 'JSON');
	}
	
	
	/**
	 * 找回密码
	 */
	window.loginpage.iforgot = function() {
		var iforgotClickBtn = $(iforgotBtn).bind("click", iforgotBtnClick = function(){
			var iforgotAccount = $("input[name=iforgotAccount]").val() ;
			var iforgotCaptchaCode = $("input[name=iforgotCaptchaCode]").val() ;
			
			if(iforgotAccount.trim().length == 0) {
				alert("不能为空");return ;
			}
			if(iforgotCaptchaCode.trim().length == 0) {
				alert("不能为空");return ;
			}
			
			$.ajax({
				type : "GET",
				url : $.ctx+"/manager/system/user/iForgotPassword/"+iforgotAccount+"/"+iforgotCaptchaCode,
				beforeSend: function() {
					iforgotClickBtn.unbind();
					$(iforgotMsg).hide();
					$(iforgotBtn).children("i").attr("class", "fa fa-spinner fa-pulse").siblings("span").html("发送中...");
				},
				success : function(result) {
					if(result.status) {
						$(iforgotBtn).removeClass("btn-1").addClass("btn-2").children("i").attr("class", "fa fa-check").siblings("span").html("已发送 <span id='count'>8</span>");
						$(iforgotMsg).removeClass("iforgot-msg-error").addClass("iforgot-msg-success").show().html(result.message);
						//5秒后重新绑定点击事件
						countTime = setInterval(aa, 1000);
						
					} else {
						$(iforgotBtn).children("i").attr("class", "fa fa-envelope").siblings("span").html("发送");
						$(iforgotMsg).removeClass("iforgot-msg-success").addClass("iforgot-msg-error").show().html(result.message);
						iforgotClickBtn.bind("click", iforgotBtnClick);
					}
				},
				complete: function () {},
				error: function (data) {
					$(iforgotBtn).children("i").attr("class", "fa fa-envelope").siblings("span").html("发送");
					$(iforgotMsg).removeClass("iforgot-msg-success").addClass("iforgot-msg-error").show().html("发生错误["+data.status+"]");
					iforgotClickBtn.bind("click", iforgotBtnClick);
				}
			});
		});
		
		function aa() {
			var count = $("#count") ;
			var t = parseInt(count.text()) ;
			count.html((t-1));
			if(t == 1) {
				count.remove();
				window.clearInterval(countTime);
				iforgotClickBtn.bind("click", iforgotBtnClick);
				$(iforgotBtn).removeClass("btn-2").addClass("btn-1");
				$(iforgotBtn).children("i").attr("class", "fa fa-envelope").siblings("span").html("发送");
				$(iforgotMsg).hide();
			}
		}
	};
	
	window.loginpage.remember = function(){
		var loginUser = $.cookie(cookieUser);
		if (undefined != loginUser && "" != loginUser) {
			var splits = loginUser.split("[#]");
			$("input[type=text][name=account]").val($.base64.decode(splits[0]));
			$("input[type=password][name=password]").val($.base64.decode(splits[1]));
			$(remember).prop("checked", true);
		} else {
			$("input[type=text][name=account]").val("");
			$("input[type=password][name=password]").val("");
			$(remember).prop("checked", false);
		}
	};
	window.loginpage.setCookie = function(name, value) {
		$.cookie(name, value, {
			isStrict: true,
			expires : 30,
			path : "/"
		});
	};
	window.loginpage.removeCookie = function(name) {
		$.cookie(name, null, {
			expires : -1,
			path : "/"
		});
	};
	
	$(function(){
		window.loginpage.init();
		window.loginpage.captchaImage();
		window.loginpage.clickLogin();
		window.loginpage.keydownLogin();
		window.loginpage.remember();
		window.loginpage.iforgot();
	});
})(jQuery);